{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "27ba2719",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jarque-Bera统计量: 1048.0054\n",
      "P值: 0.0000\n",
      "结论: 拒绝原假设，数据不服从正态分布\n"
     ]
    }
   ],
   "source": [
    "import scipy.stats as stats\n",
    "\n",
    "def jarque_bera_diagnostic(skewness, excess_kurtosis, n):\n",
    "    \"\"\"\n",
    "    执行Jarque-Bera正态性检验\n",
    "    \n",
    "    参数:\n",
    "        skewness (float): 数据的偏度\n",
    "        excess_kurtosis (float): 数据的超额峰度（即峰度 - 3）\n",
    "        n (int): 样本数量\n",
    "    \n",
    "    返回:\n",
    "        jb_stat (float): Jarque-Bera统计量\n",
    "        p_value (float): 检验的p值\n",
    "    \"\"\"\n",
    "    # 计算Jarque-Bera统计量\n",
    "    jb_stat = (n / 6) * (skewness**2 + (excess_kurtosis-3)**2 / 4)\n",
    "    \n",
    "    # 计算p值（使用卡方分布的生存函数）\n",
    "    p_value = stats.chi2.sf(jb_stat, df=2)\n",
    "    \n",
    "    return jb_stat, p_value\n",
    "\n",
    "# 示例输入（替换为你的实际数据）\n",
    "skew = -0.0971792174       # 偏度\n",
    "excess_kurt = 11.3914463891  # 超额峰度（峰度 - 3）\n",
    "sample_size = 357 # 样本数量\n",
    "\n",
    "# 执行检验\n",
    "jb_statistic, p = jarque_bera_diagnostic(skew, excess_kurt, sample_size)\n",
    "\n",
    "# 输出结果\n",
    "print(f\"Jarque-Bera统计量: {jb_statistic:.4f}\")\n",
    "print(f\"P值: {p:.4f}\")\n",
    "\n",
    "# 以α=0.05为显著性水平判断\n",
    "alpha = 0.05\n",
    "if p < alpha:\n",
    "    print(\"结论: 拒绝原假设，数据不服从正态分布\")\n",
    "else:\n",
    "    print(\"结论: 无法拒绝原假设，数据可能服从正态分布\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "myenv2",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
